HTMLForm: Clean up 0 handling
authorBrad Jorsch <bjorsch@wikimedia.org>
Wed, 5 Mar 2014 20:15:41 +0000 (15:15 -0500)
committerBrad Jorsch <bjorsch@wikimedia.org>
Wed, 5 Mar 2014 20:40:48 +0000 (15:40 -0500)
PHP, particularly with in_array, really has problems with integer 0
versus non-numeric strings. Let's clean that up by converting values to
strings more agressively and using the $strict option to in_array.

Oddly enough, the one place where strict in_array was being used already
broke when If4175332 stringified the values in one array but not the
other.

Bug: 62268
Change-Id: Id34e654eb8d0e70d093b11445273e542e491e265

includes/htmlform/HTMLMultiSelectField.php
includes/htmlform/HTMLRadioField.php
includes/htmlform/HTMLSelectAndOtherField.php
includes/htmlform/HTMLSelectField.php
includes/htmlform/HTMLSelectOrOtherField.php

index 2944c24..3cf3188 100644 (file)
@@ -28,6 +28,7 @@ class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable
        }
 
        function getInputHTML( $value ) {
+               $value = HTMLFormField::forceToStringRecursive( $value );
                $html = $this->formatOptions( $this->getOptions(), $value );
 
                return $html;
@@ -103,11 +104,12 @@ class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable
        }
 
        function filterDataForSubmit( $data ) {
+               $data = HTMLFormField::forceToStringRecursive( $data );
                $options = HTMLFormField::flattenOptions( $this->getOptions() );
 
                $res = array();
                foreach ( $options as $opt ) {
-                       $res["$opt"] = in_array( $opt, $data );
+                       $res["$opt"] = in_array( $opt, $data, true );
                }
 
                return $res;
index aa0ea5d..c52f0a8 100644 (file)
@@ -17,7 +17,7 @@ class HTMLRadioField extends HTMLFormField {
 
                $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
 
-               if ( in_array( $value, $validOptions ) ) {
+               if ( in_array( strval( $value ), $validOptions, true ) ) {
                        return true;
                } else {
                        return $this->msg( 'htmlform-select-badoption' )->parse();
@@ -33,7 +33,7 @@ class HTMLRadioField extends HTMLFormField {
         * @return String
         */
        function getInputHTML( $value ) {
-               $html = $this->formatOptions( $this->getOptions(), $value );
+               $html = $this->formatOptions( $this->getOptions(), strval( $value ) );
 
                return $html;
        }
@@ -51,7 +51,7 @@ class HTMLRadioField extends HTMLFormField {
                                $html .= $this->formatOptions( $info, $value );
                        } else {
                                $id = Sanitizer::escapeId( $this->mID . "-$info" );
-                               $radio = Xml::radio( $this->mName, $info, $info == $value, $attribs + array( 'id' => $id ) );
+                               $radio = Xml::radio( $this->mName, $info, $info === $value, $attribs + array( 'id' => $id ) );
                                $radio .= '&#160;' . call_user_func( $elementFunc, 'label', array( 'for' => $id ), $label );
 
                                $html .= ' ' . Html::rawElement(
index 2e91a23..564927f 100644 (file)
@@ -72,7 +72,7 @@ class HTMLSelectAndOtherField extends HTMLSelectField {
 
                        if ( $list == 'other' ) {
                                $final = $text;
-                       } elseif ( !in_array( $list, $this->mFlatOptions ) ) {
+                       } elseif ( !in_array( $list, $this->mFlatOptions, true ) ) {
                                # User has spoofed the select form to give an option which wasn't
                                # in the original offer.  Sulk...
                                $final = $text;
index 0437480..c32b445 100644 (file)
@@ -13,7 +13,7 @@ class HTMLSelectField extends HTMLFormField {
 
                $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
 
-               if ( in_array( $value, $validOptions ) ) {
+               if ( in_array( strval( $value ), $validOptions, true ) ) {
                        return true;
                } else {
                        return $this->msg( 'htmlform-select-badoption' )->parse();
index 045b8df..e8bcb5b 100644 (file)
@@ -21,7 +21,10 @@ class HTMLSelectOrOtherField extends HTMLTextField {
                $valInSelect = false;
 
                if ( $value !== false ) {
-                       $valInSelect = in_array( $value, HTMLFormField::flattenOptions( $this->getOptions() ) );
+                       $value = strval( $value );
+                       $valInSelect = in_array(
+                               $value, HTMLFormField::flattenOptions( $this->getOptions() ), true
+                       );
                }
 
                $selected = $valInSelect ? $value : 'other';
@@ -67,7 +70,7 @@ class HTMLSelectOrOtherField extends HTMLTextField {
                if ( $request->getCheck( $this->mName ) ) {
                        $val = $request->getText( $this->mName );
 
-                       if ( $val == 'other' ) {
+                       if ( $val === 'other' ) {
                                $val = $request->getText( $this->mName . '-other' );
                        }